/*
 * @Author: szx
 * @Date: 2022-03-08 13:59:34
 * @LastEditTime: 2022-03-08 14:18:39
 * @Description:
 * @FilePath: \leetcode\500-599\525\525.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxLength = function (nums) {
    const map = new Map();
    map.set(0, -1);
    let sum = 0;
    let res = 0;
    for (let i = 0; i < nums.length; i++) {
        sum += nums[i] == 1 ? 1 : -1;
        if (map.has(sum)) {
            res = Math.max(i - map.get(sum), res);
        } else {
            map.set(sum, i);
        }
    }
    return res;
};
console.log(findMaxLength([0, 1]));
console.log(findMaxLength([0, 1, 0, 1]));
